import { ref } from 'vue'
import { ElMessage } from "element-plus";

export function useDevice() {
  const hasAudioInput = ref(false);
  const hasVideoInput = ref(false);
  const hasScreenInput = ref(false);
  const videoRef = ref<HTMLVideoElement>();
  const tip = ref('摄像头处于关闭状态');

  // 修复类型定义
  const localStream = {
    videoStream: null as MediaStream | null,
    audioStream: null as MediaStream | null,
    screenStream: null as MediaStream | null
  }

  async function init() {
    const devices = await navigator.mediaDevices.enumerateDevices();
    const audioInput = devices.find(d => d.kind === 'audioinput');
    const videoInput = devices.find(d => d.kind === 'videoinput');
    if (!audioInput) {
      ElMessage({
        message: '没有找到麦克风',
        type: 'warning'
      })
    }
    if (!videoInput) {
      ElMessage({
        message: '没有找到摄像头',
        type: 'warning'
      })
    }
  }

  init();

  async function openMicro() {
    console.log('打开麦克风');
    try {
      localStream.audioStream = await navigator.mediaDevices.getUserMedia({ audio: true });
      if (videoRef.value) {
        hasAudioInput.value = true;
      }
    } catch (error) {
      console.error('麦克风访问失败:', error);
    }
  }

  async function closeMicro() {
    console.log('关闭麦克风');
    localStream.audioStream?.getTracks().forEach(track => track.stop());
    localStream.audioStream = null;
    hasAudioInput.value = false;
  }

  async function openVideo() {
    if(hasScreenInput.value===true){
      ElMessage({
        message: '屏幕共享中，无法打开摄像头',
        type: 'warning'
      })
      return;
    }
    console.log('打开摄像头');
    try {
      localStream.videoStream = await navigator.mediaDevices.getUserMedia({ video: true });
      if (videoRef.value) {
        videoRef.value.srcObject = localStream.videoStream;
        hasVideoInput.value = true;
        tip.value = '摄像头处于开启状态';
      }
    } catch (error) {
      ElMessage({
        message: '摄像头访问失败: '+error,
        type: 'warning'
      });
    }
  }

  function closeVideo() {
    console.log('关闭摄像头');
    localStream.videoStream?.getTracks().forEach(track => track.stop());
    if (videoRef.value) {
      videoRef.value.srcObject = null;
    }
    localStream.videoStream = null;
    hasVideoInput.value = false;
    tip.value = '摄像头处于关闭状态';
  }

  async function openScreen() {
    if(hasVideoInput.value===true){
      ElMessage({
        message: '摄像头开启中，无法打开屏幕共享',
        type: 'warning'
      })
      return;
    }
    console.log('打开屏幕共享');
    try {
      localStream.screenStream = await navigator.mediaDevices.getDisplayMedia({video: true});
      if (videoRef.value) {
        videoRef.value.srcObject = localStream.screenStream;
        hasScreenInput.value = true;
        hasVideoInput.value = false;
        localStream.videoStream?.getTracks().forEach(track => track.stop());
      }
    } catch (error) {
      console.error('屏幕共享失败:', error);
    }
  }

  async function closeScreen() {
    console.log('关闭屏幕共享');
    localStream.screenStream?.getTracks().forEach(track => track.stop());
    localStream.screenStream = null;
    hasVideoInput.value = false;
  }

  return {
    openMicro,
    closeMicro,
    videoRef,
    openVideo,
    closeVideo,
    openScreen,
    closeScreen,
    hasAudioInput,
    hasVideoInput,
    hasScreenInput,
    tip
  };
}
